Package com.gui

Source Code of com.gui.MainPanel

package com.gui;
/**
* @author Dror
*
* email: gumjum.o.o@gmail.com
*
*/



import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.GroupLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;

import com.fileio.FileIO;
import com.fish.Fish;
import com.fish.World;

public class MainPanel extends JPanel implements MouseListener, ActionListener{
 
  /**
   *
   */
  private static final long serialVersionUID = 1L;
 
  public static int SCR_HEIGHT;
  public static int SCR_WIDTH;
 
  Menu curMenu;
  public MainMenu mainMenu;
  ShopMenu shopMenu;
  SimulationMenu simMenu;
  FishInfoWindow fiwindow;
  CameraMenu camMenu;
 
  long time,lastTime;
  float fps;
  int frames = 0;
  boolean busy = false;
 


  Timer timer;
 
  GroupLayout layout;
 
 
 
  public MainPanel(JFrame win) {
    super();
   
    FileIO.loadXMLResources();
   
    System.out.println("building main-panel...");
    System.out.println("screen size: "+SCR_HEIGHT + "," +SCR_WIDTH);
   
    this.setBackground(Color.gray);
    this.setPreferredSize(new Dimension(SCR_WIDTH,SCR_HEIGHT));
    this.setVisible(true);
//    this.setDoubleBuffered(true);
   
    Menu.setFrameAndPanel(this, win);
   
    mainMenu = new MainMenu();
    shopMenu = new ShopMenu();
    simMenu = new SimulationMenu();
    camMenu = new CameraMenu();
    fiwindow = new FishInfoWindow();
   
    curMenu = mainMenu;
   
    layout = new GroupLayout(this);
    this.setLayout(layout);
    layout.setAutoCreateGaps(true);
    layout.setAutoCreateContainerGaps(true);
   
    layout.setVerticalGroup(layout.createSequentialGroup()
        .addComponent(curMenu)
        .addGroup(layout.createParallelGroup()
        .addComponent(fiwindow)));
   
    layout.setHorizontalGroup(layout.createParallelGroup()
        .addComponent(curMenu)
        .addGroup(layout.createSequentialGroup()
        .addComponent(fiwindow)));

   
    World.fromXML(FileIO.readXMLFile(World.def));
    World.startSimulation();
   
    lastTime = 0;
    time = System.currentTimeMillis();
    timer = new Timer(100/3, this); //30 ticks per second
    timer.start();
  }
 
  public void paintComponent(Graphics g)
  {
    super.paintComponent(g);
   
    World.show(g)

    g.setColor(Color.black);
    g.fillRect(SCR_WIDTH/2 - 8, SCR_HEIGHT/2 - 1,16,2);
    g.fillRect(SCR_WIDTH/2 - 1, SCR_HEIGHT/2 - 8,2,16);
   
    time = System.currentTimeMillis();
    if (time - lastTime < 1000) {
      frames++;
    }
    else {
      fps = frames;
      lastTime = time;
      frames = 0;
    }

  }
 
  public void mouseClicked(MouseEvent e) {
    if(e.getButton() == MouseEvent.BUTTON1){
      Fish f = World.clicked(e.getX(),e.getY());
     
      if(f != null){
        fiwindow.clickedOnFish(f);
      }
    }else if(e.getButton() == MouseEvent.BUTTON3){

    }else if(e.getButton() == MouseEvent.BUTTON3){
     
    }
   
  }

  @Override
  public void mouseEntered(MouseEvent arg0) {
    // TODO Auto-generated method stub
   
  }

  @Override
  public void mouseExited(MouseEvent arg0) {
    // TODO Auto-generated method stub
   
  }

  @Override
  public void mousePressed(MouseEvent arg0) {
    // TODO Auto-generated method stub
   
  }

  @Override
  public void mouseReleased(MouseEvent arg0) {
    // TODO Auto-generated method stub
   
  }

 
  public void actionPerformed(ActionEvent e) {
   
   
    if(!busy){
      busy = true;
      if(World.running){
        World.step();
      }else{
        World.cam.step();
      }
      repaint();
      busy = false;
    }
  }




}
TOP

Related Classes of com.gui.MainPanel

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.